package net.qhzw.vip.qiyu.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import net.qhzw.vip.qiyu.entity.RepairMaint;
import net.qhzw.vip.qiyu.entity.SysNews;
import net.qhzw.vip.qiyu.handler.MyException;
import net.qhzw.vip.qiyu.result.JsonResult;
import net.qhzw.vip.qiyu.service.SysNewsService;
import net.qhzw.vip.qiyu.util.SecurityUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("sys/news")
@Api(tags = "骑遇新闻管理")
public class SysNewsController {


    @Autowired
    private SysNewsService sysNewsService;


    @ApiOperation(value = "分页查询新闻")
    @GetMapping
    public JsonResult<Page<SysNews>> findByPage(Page<SysNews> page, String title, Byte status, Byte enable) {
        page.addOrder(OrderItem.desc(""));
        Page<SysNews> sysNewsPage = sysNewsService.page(page, new LambdaQueryWrapper<SysNews>()
                .eq(status != null, SysNews::getStatus, status)
                .eq(enable != null, SysNews::getEnable, enable)
                .like(!StringUtils.isEmpty(title), SysNews::getTitle, title)
                .last("ORDER BY `enable`,created DESC"));
        return JsonResult.ok(sysNewsPage);
    }


    @ApiOperation(value = "根据ID查询新闻")
    @GetMapping("{id}")
    public JsonResult<SysNews> findSysNews(@PathVariable Long id) {
        SysNews sysNews = sysNewsService.getById(id);
        return JsonResult.ok(sysNews);
    }


    @ApiOperation(value = "添加新闻")
    @PostMapping
    public JsonResult addSysNews(@RequestBody SysNews sysNews) {
        boolean save = sysNewsService.save(initDB(sysNews));
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    @ApiOperation(value = "修改新闻")
    @PutMapping
    public JsonResult updateSysNews(@RequestBody SysNews sysNews) {
        boolean update = sysNewsService.updateById(sysNews);
        if (update) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "设置新闻状态")
    @PutMapping("setStatus")
    public JsonResult setStatus(@RequestBody SysNews sysNews) {
        boolean save = sysNewsService.updateById(sysNews);
        if (save) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }


    @ApiOperation(value = "删除新闻")
    @DeleteMapping("{id}")
    public JsonResult<RepairMaint> findRepairMaint(@PathVariable String id) {
        List<String> list = Arrays.asList(id.split(","));
        boolean remove = sysNewsService.removeByIds(list);
        if (remove) {
            return JsonResult.ok();
        }
        return JsonResult.fail();
    }

    private SysNews initDB(SysNews sysNews) {
        if (sysNews.getId() == null) {
            int count = sysNewsService.count(new LambdaQueryWrapper<SysNews>().eq(SysNews::getTitle, sysNews.getTitle()));
            if (count > 0) {
                throw new MyException("标题已存在");
            }
            sysNews.setId(IdWorker.getId());
            sysNews.setCreated(new Date());
            sysNews.setStatus((byte) 1);
            sysNews.setCreateBy(SecurityUtils.getUserId());
            return sysNews;
        }
        return sysNews;
    }


}
